package com.jifeng.sterilizer.service.impl;

import com.jifeng.sterilizer.entity.LiquidTypeEntity;
import com.jifeng.sterilizer.service.LiquidTypeService;
import com.jifeng.sterilizer.utils.R;
import com.jifeng.sterilizer.utils.RegexUtils;
import com.jifeng.sterilizer.vo.LiquidEntityVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jifeng.sterilizer.utils.PageUtils;
import com.jifeng.sterilizer.utils.Query;

import com.jifeng.sterilizer.dao.LiquidDao;
import com.jifeng.sterilizer.entity.LiquidEntity;
import com.jifeng.sterilizer.service.LiquidService;

import javax.validation.constraints.Null;
import javax.xml.crypto.Data;


@Service("liquidService")
@Slf4j
public class LiquidServiceImpl extends ServiceImpl<LiquidDao, LiquidEntity> implements LiquidService {

    @Autowired
    LiquidTypeService liquidTypeService;

    /**
     * @param params {type: '', id: '', status: '', produce_time: '',sale_time: '', useTime: '',page_number,page_size}
     * @return {code:"200",msg:"成功",data: {total:xx,pageNumber:xx,pageSize:xx,
     * list:[{farm: '', type: '', id: '', status: '', createTime: '', updataTime: '', useTime: ''}]}
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        try {
            QueryWrapper<LiquidEntity> queryWrapper = new QueryWrapper<>();
            Object name = params.get("name");
            Object type = params.get("type");
            Object id = params.get("id");
            Object status = params.get("status");
            Object produce_time = params.get("produce_time");
            Object sale_time = params.get("sale_time");
            Object useTime = params.get("useTime");
            if (!name.equals("")) {
                queryWrapper.like("name", name);
            }
            if (!type.equals("")) {
                queryWrapper.eq("liquid_type_id", type);
            }
            if (!id.equals("")) {
                queryWrapper.eq("number", id);
            }
            if (!status.equals("")) {
                queryWrapper.eq("status", status);
            }
            if (!produce_time.equals("")) {
                queryWrapper.like("produce_time", produce_time);
            }
            if (!sale_time.equals("")) {
                queryWrapper.like("sale_time", sale_time);
            }
            if (!useTime.equals("")) {
                queryWrapper.like("use_time", useTime);
            }
            IPage<LiquidEntity> page = this.page(
                    new Query<LiquidEntity>().getPage(params),
                    queryWrapper
            );
            List<LiquidEntity> records = page.getRecords();
            List<LiquidEntity> collect = records.stream().map(liquidEntity -> {
                LiquidTypeEntity liquidTypeEntity = liquidTypeService.getById(liquidEntity.getLiquidTypeId());
                liquidEntity.setLiquidTypeName(liquidTypeEntity.getName());
                return liquidEntity;
            }).collect(Collectors.toList());
            page.setRecords(collect);
            return new PageUtils(page);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "后台药剂搜索", e.getMessage());
            return null;
        }
    }

    @Override
    public R getInfo(Map<String, Object> params) {
        try {
            Map<String, Object> map = new HashMap<>();
            Map<String, Object> map2 = new HashMap<>();
            Object liquid_id = params.get("liquid_id");
            boolean isLiquidNumber = RegexUtils.isNumber(String.valueOf(liquid_id), 0);
            if (!isLiquidNumber) {
                log.warn("方法：{}，错误信息：{}", "获取药剂信息", "药剂编号格式错误");
                return R.error(400, "药剂编号格式错误");
            }
            LiquidEntity liquidEntity = this.getOne(new QueryWrapper<LiquidEntity>().eq("number", liquid_id));
            if (liquidEntity == null) {
                log.warn("方法：{}，错误信息：{}", "获取药剂信息", "药剂编号无效,未找到药剂");
                return R.error(400, "药剂编号无效,未找到药剂");
            }
            Integer status = liquidEntity.getStatus();
            Long liquidTypeId = liquidEntity.getLiquidTypeId();
            LiquidTypeEntity liquidTypeEntity = liquidTypeService.getOne(new QueryWrapper<LiquidTypeEntity>().eq("id", liquidTypeId));
            if (liquidTypeEntity == null) {
                log.warn("方法：{}，错误信息：{}", "获取药剂信息", "药剂类型id无效,未找到药剂类型");
                return R.error(400, "药剂类型id无效,未找到药剂类型");
            }
            String name = liquidTypeEntity.getName();
            Float radio = liquidTypeEntity.getRatio();
            Float capacity = liquidTypeEntity.getCapacity();
            map2.put("name", name);
            map2.put("radio", radio);
            map2.put("capacity", capacity);
            map.put("type", map2);
            map.put("status", status);
            map.put("produceTime", liquidEntity.getProduceTime());
            map.put("saleTime", liquidEntity.getSaleTime());
            map.put("useTime", liquidEntity.getUseTime());
            return R.ok().put("data", map);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "获取药剂信息", e.getMessage());
            return R.error(500, "系统繁忙");
        }
    }

    @Override
    public R changeTime(Map<String, Object> params) {
        try {
            Integer type = Integer.valueOf(String.valueOf(params.get("type")));
            Object id = params.get("id");
            if (!RegexUtils.isNumber(id.toString(),0)){
                log.warn("方法：{}，错误信息：{}", "修改药剂出厂或使用时间", "药剂编号格式错误");
                return R.error(400, "药剂编号格式错误");
            }
            LiquidEntity liquidEntity = new LiquidEntity();
            if (type == 0) {
                liquidEntity.setSaleTime(new Date());
            } else if (type == 1) {
                liquidEntity.setUseTime(new Date());
            } else {
                return R.error(400, "无效的type类型");
            }
            boolean update = this.update(liquidEntity, new QueryWrapper<LiquidEntity>().eq("number", id));
            if (update) {
                return R.ok();
            }
            return R.error();
        } catch (Exception e) {
            e.printStackTrace();
            log.error("方法：{}，错误信息：{}", "修改药剂出厂或使用时间", e.getMessage());
            return R.error(400, "系统繁忙");
        }
    }

    @Override
    public R del(Map<String, Object> params) {
        Object id = params.get("id");
        boolean remove = this.removeById((Serializable) id);
        if (remove) {
            return R.ok();
        }
        return R.error(400, "无效的药剂id");
    }

    /**
     * @param params {"number":333,"liquid_type_id":1,"status":1,"note":"bbb","name":"4号药剂"}
     * @return
     */
    @Override
    public R add(Map<String, Object> params) {
        String number = String.valueOf(params.get("number"));
        Long liquid_type_id = Long.valueOf(String.valueOf(params.get("liquid_type_id")));
        Integer status = Integer.valueOf(String.valueOf(params.get("status")));
        String note = String.valueOf(params.get("note"));
        String name = String.valueOf(params.get("name"));
        LiquidEntity liquidEntity = new LiquidEntity();
        liquidEntity.setNumber(number);
        liquidEntity.setLiquidTypeId(liquid_type_id);
        liquidEntity.setStatus(status);
        liquidEntity.setMemo(note);
        liquidEntity.setName(name);
        boolean save = this.save(liquidEntity);
        if (save) {
            return R.ok();
        }
        return R.error();
    }

    /**
     * @param params {id: 1,number:'0123456789',liquid_type_id: '', status: '', note:'' ,name:""}
     * @return
     */
    @Override
    public R myUpdate(Map<String, Object> params) {
        Object id = params.get("id");
        String number = String.valueOf(params.get("number"));
        Long liquid_type_id = Long.valueOf(String.valueOf(params.get("liquid_type_id")));
        Integer status = Integer.valueOf(String.valueOf(params.get("status")));
        String note = String.valueOf(params.get("note"));
        String name = String.valueOf(params.get("name"));
        LiquidEntity liquidEntity = new LiquidEntity();
        liquidEntity.setNumber(number);
        liquidEntity.setLiquidTypeId(liquid_type_id);
        liquidEntity.setStatus(status);
        liquidEntity.setMemo(note);
        liquidEntity.setName(name);
        boolean update = this.update(liquidEntity, new QueryWrapper<LiquidEntity>().eq("id", id));
        if (update) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}